<template>
  <el-drawer :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="110px"
      v-loading="formLoading"
    >
      <el-form-item label="餐桌号" prop="no">
        <el-input v-model="formData.no" placeholder="请输入餐桌号，如：A01" class="!w-240px" />
      </el-form-item>
      <el-form-item label="餐桌分类" prop="categoryId">
        <el-select
          v-model="formData.categoryId"
          placeholder="选择餐桌分类"
          class="w-240px"
          clearable
        >
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片" prop="picUrl">
        <UploadImg v-model="formData.picUrl" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts" name="DeskForm">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as DeskApi from '@/api/dine/desk/DeskApi'
import * as DeskCategoryApi from '@/api/dine/desk/DeskCategoryApi'
import { useShopStore } from '@/store/modules/shop'

const { t } = useI18n()
const message = useMessage()

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref<any>({
  id: undefined,
  no: undefined,
  categoryId: undefined,
  picUrl: undefined
})
const formRules = reactive({
  no: [{ required: true, message: '餐桌号不能为空', trigger: 'blur' }],
  categoryId: [{ required: true, message: '餐桌分类不能为空', trigger: 'blur' }]
})
const formRef = ref()

const categoryList = ref<any>()

const shopStore = useShopStore()
const open = async (type: string, id?: number | string) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DeskApi.getDetailById(id)
    } finally {
      formLoading.value = false
    }
  }
  let shopId = formData.value.shopId
  if (!shopId) {
    await shopStore.setShopInfoAction()
    shopId = shopStore.shopInfo?.id || '0'
  }
  categoryList.value = await DeskCategoryApi.getAll(shopId)
  //console.table(categoryList.value)
}
defineExpose({ open })

const emit = defineEmits(['success'])
const submitForm = async () => {
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await DeskApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await DeskApi.updateById(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const resetForm = () => {
  formData.value = {
    id: undefined,
    no: undefined,
    categoryId: undefined,
    picUrl: undefined
  }
  formRef.value?.resetFields()
}
</script>
